package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.model.product.BaseCategory1;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.product.service.ManageService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品基础属性接口")
@RestController
@RequestMapping("admin/product")
public class BaseManageController {

    @Autowired
    private ManageService manageService;

    /**
     * 获取一级分类数据，查询所有的一级分类信息
     *
     * @return
     */
    @GetMapping("getCategory1")
    public Result getCategory1() {
        //获取服务层数据
        List<BaseCategory1> baseCategory1List = manageService.getCategory1();
        //依据前端数据接口，返回对应的result.data数据
        return Result.ok(baseCategory1List);
    }

    @GetMapping("getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable("category1Id") Long category1Id) {

        return Result.ok(manageService.getCategory2(category1Id));
    }

    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable("category2Id") Long category2Id) {
        return Result.ok(manageService.getCategory3(category2Id));
    }

    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getattrInfoList(@PathVariable("category1Id") Long category1Id,
                                  @PathVariable("category2Id") Long category2Id,
                                  @PathVariable("category3Id") Long category3Id
    ) {
        List<BaseAttrInfo> attrInfoList = manageService.getAttrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(attrInfoList);
    }


    //http://api.gmall.com/admin/product/saveAttrInfo

    /**
     * 保存平台属性方法,添加
     *
     * @param baseAttrInfo
     * @return
     * @RequestBody json-->javaObject
     */
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        //调用服务处方法
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }
    //根据平台属性id，获取平台属性值集合，回显
    //http://api.gmall.com/admin/product/getAttrValueList/{attrId}

    @GetMapping("getAttrValueList/{attrId}")
    /**
     * 数据回显
     * @param attrId
     * @return
     */
    public Result getAttrValueList(@PathVariable Long attrId) {

        BaseAttrInfo baseAttrInfo = manageService.getAttrInfo(attrId);

//        List<BaseAttrValue> baseAttrValueList = manageService.getAttrValueList(attrId);
//        return Result.ok(baseAttrValueList);
        return Result.ok(baseAttrInfo.getAttrValueList());
    }


}
